package com.szholly.pro.watergas_server.ps.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.ITable;
import com.szholly.data.general.ITableFactory;

@Controller
@RequestMapping("/VideoController/")
public class VideoController extends BaseController {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	/**
	 * 根据视频位置获取站点视频数据
	 */
	@RequestMapping(value="/getStationData")
	public void getStationData(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		String station_id=RequestUnit.getParameter("station_id");
		String WWTP_ID=RequestUnit.getParameter("WWTP_ID");
		String station_category=RequestUnit.getParameter("station_category");
		String code=RequestUnit.getParameter("STATION_ID_LIKE");
		String name=RequestUnit.getParameter("STATION_NAME");
		String VIDEO_POSITION=RequestUnit.getParameter("VIDEO_POSITION");
		String FLOW_TYPE=RequestUnit.getParameter("FLOW_TYPE");
		String sql="  select t.*,t2.ip,t2.USER_ID,t2.PASSWORD,t2.PORT,t2.VIDEO_ID,t2.VIDEO_POSITION from BC_SITE_INFO t left join BC_VIDEO_INFO t2 on t.exp_no=t2.station_code left join BC_ENTERPRISE_ATTR t3 on t.wwtp_id=t3.exp_no where 1=1 "
				+ "  and t2.ip is not null ";
		if (WWTP_ID != null && !WWTP_ID.equals("")) {
			sql+= " and t.WWTP_ID = '" +WWTP_ID + "'";
		}
		if (station_id != null && !station_id.equals("")) {
			sql+= " and t.exp_no = '" +station_id + "'";
		}
		if (station_category != null && !station_category.equals("")) {
			sql+= " and t.station_category = '" +station_category + "'";
		}
		if (code != null && !code.equals("")) {
			sql+= " and t.code like '__" +code + "%'";
		}
		if (name != null && !name.equals("")) {
			sql+= " and t3.name like '%" +name + "%'";
		}
		if (VIDEO_POSITION != null && !VIDEO_POSITION.equals("")) {
			sql+= " and t2.VIDEO_POSITION = '" +VIDEO_POSITION + "'";
		}
		if (FLOW_TYPE != null && !FLOW_TYPE.equals("")) {
			sql+= " and t.FLOW_TYPE = '" +FLOW_TYPE + "'";
		}
		List<GlobalParameter> parm = new ArrayList<GlobalParameter>();
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("BC_ENTERPRISE_ATTR");
		String result = DataUtil.getPageSelectJson(sql, parm, pTable);
		outString(result);
	}
	/**
	 * 根据视频位置获取质控公司视频数据
	 */
	@RequestMapping(value="/getCompanyData")
	public void getCompanyData(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		String station_id=RequestUnit.getParameter("station_id");
		String name=RequestUnit.getParameter("STATION_NAME");
		String VIDEO_POSITION=RequestUnit.getParameter("VIDEO_POSITION");
		String sql="  select t.*,t2.ip,t2.USER_ID,t2.PASSWORD,t2.PORT,t2.VIDEO_ID,t2.VIDEO_POSITION from qc_companyinfo t left join BC_VIDEO_INFO t2 on t.companycode=t2.station_code left join BC_ENTERPRISE_ATTR t3 on t.companycode=t3.exp_no where 1=1 "
				+ "  and t2.ip is not null ";
		if (station_id != null && !station_id.equals("")) {
			sql+= " and t.companycode = '" +station_id + "'";
		}
		if (name != null && !name.equals("")) {
			sql+= " and t3.name like '%" +name + "%'";
		}
		if (VIDEO_POSITION != null && !VIDEO_POSITION.equals("")) {
			sql+= " and t2.VIDEO_POSITION = '" +VIDEO_POSITION + "'";
		}
		List<GlobalParameter> parm = new ArrayList<GlobalParameter>();
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("BC_ENTERPRISE_ATTR");
		String result = DataUtil.getPageSelectJson(sql, parm, pTable);
		outString(result);
	}
	/**
	 * 获取有视频污水厂
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="/getWWTPData")
	public void getWWTPData(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		String station_category=RequestUnit.getParameter("station_category");
		String code=RequestUnit.getParameter("STATION_ID_LIKE");
		String name=RequestUnit.getParameter("STATION_NAME");
		String companyid=RequestUnit.getParameter("COMPANYID");
		String sql="  select t3.name ,t3.exp_no,t3.img as photo,t3.temp_img from BC_ENTERPRISE_ATTR t3 where 1=1  ";
		if (station_category != null && !station_category.equals("")) {
			sql+= " and t3.POLL_TYPE = '" +station_category + "'";
		}
		if (code != null && !code.equals("")) {
			sql+= " and t3.code like '__" +code + "%'";
		}
		if (name != null && !name.equals("")) {
			sql+= " and t3.name like '%" +name + "%'";
		}
		if(companyid!=null && !companyid.equals("")){
			sql+=" and t3.exp_no in("+ParamPartition(companyid)+")";	
		}
		sql+=" group by t3.name,t3.exp_no,t3.img,t3.temp_img order by t3.name";
		
		List<GlobalParameter> parm = new ArrayList<GlobalParameter>();
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("BC_ENTERPRISE_ATTR");
		String result = DataUtil.getPageSelectJson(sql, parm, pTable);
		outString(result);
	}
	
	/**
	 * 根据公司获取DVR信息
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="/getCompanyDVRInfo")
	public void getCompanyDVRInfo(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		String wwtp_id=RequestUnit.getParameter("EXP_NO");
		String sql=" select t1.* from BC_DVR_INFO t1 left join qc_companyinfo t2 on t1.station_code=t2.companycode where 1=1 ";
		if (wwtp_id != null && !wwtp_id.equals("")) {
			sql+= " and t2.companycode = '" +wwtp_id + "'";
		}
		List<GlobalParameter> parm = new ArrayList<GlobalParameter>();
		String result = DataUtil.getPageSelectJson(sql, parm);
		outString(result);
	}
	/**
	 * 根据污水厂获取DVR信息
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="/getDVRInfo")
	public void getDVRInfo(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		String wwtp_id=RequestUnit.getParameter("EXP_NO");
		String sql=" select t1.* from BC_DVR_INFO t1 left join BC_SITE_INFO t2 on t1.station_code=t2.exp_no where 1=1 ";
		if (wwtp_id != null && !wwtp_id.equals("")) {
			sql+= " and t2.wwtp_id = '" +wwtp_id + "'";
		}
		List<GlobalParameter> parm = new ArrayList<GlobalParameter>();
		String result = DataUtil.getPageSelectJson(sql, parm);
		outString(result);
	}
	@RequestMapping(value="/getWsclcData")
	public void getWsclcData(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		String sql=" select name,exp_no from BC_ENTERPRISE_ATTR where POLL_TYPE='0' ";
		List<GlobalParameter> parm = new ArrayList<GlobalParameter>();
		String result = DataUtil.getPageSelectJson(sql, parm);
		outString(result);
	}
	@RequestMapping(value="/geQcStation")
	public void geQcStation(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		String sql=" select name,exp_no from BC_ENTERPRISE_ATTR where POLL_TYPE='8' ";
		List<GlobalParameter> parm = new ArrayList<GlobalParameter>();
		String result = DataUtil.getPageSelectJson(sql, parm);
		outString(result);
	}
	public String ParamPartition(String paramData){
		String[] param=paramData.split(",");
		String StrParam="";
		for(int i=0;i<param.length;i++){
			if(param.length-i==1){
				StrParam+="'"+param[i]+"'";
			}else{
				StrParam+="'"+param[i]+"',";
			}			
		}
		return StrParam;
	}
} 
